package com.lanlian.uikit;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 14:25
 *     desc   : 基本对话框样式
 *     version: 1.0
 * </pre>
 */
public class CustomDialog extends Dialog {

    private OnClickListener onClickListener;

    private TextView mTvTitle;
    private TextView mTvContent;
    private TextView mTvLeft;
    private View mView;
    private TextView mTvRight;
    private LinearLayout mLay;

    private Context context;
    private String title = "提示";
    private String content = "";
    private String left = "取消";
    private String right = "确定";
    private int leftColor;
    private int rightColor;
    private boolean oneBtn = false;
    private int image = 0;
    private int gravity = 0;
    private int width;

    public CustomDialog(Context context, OnClickListener onClickListener) {
        super(context, R.style.customDialog);
        this.context = context;
        this.onClickListener = onClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom);
        initView();

        if (width != 0) {
            ViewGroup.LayoutParams lp = mLay.getLayoutParams();
            lp.width = width;
            mLay.setLayoutParams(lp);
        }

        if (leftColor != 0) {
            mTvLeft.setTextColor(context.getResources().getColor(leftColor));
        }
        if (rightColor != 0) {
            mTvRight.setTextColor(context.getResources().getColor(rightColor));
        }
        if (oneBtn) {
            mView.setVisibility(View.GONE);
            mTvLeft.setVisibility(View.GONE);
        } else {
            mView.setVisibility(View.VISIBLE);
            mTvLeft.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(content)) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTvTitle.getLayoutParams();
            lp.topMargin = context.getResources().getDimensionPixelSize(R.dimen.value_25dp);
            mTvContent.setVisibility(View.VISIBLE);
        } else {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTvTitle.getLayoutParams();
            lp.topMargin = 0;
            mTvContent.setVisibility(View.GONE);
        }
        if (image != 0) {
            mTvTitle.setCompoundDrawablesWithIntrinsicBounds(image, 0, 0, 0);
            mTvTitle.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.value_8dp));
        }
        if (gravity != 0) {
            mTvContent.setGravity(gravity);
        }
        mTvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onLeftClick();
                dismiss();
            }
        });
        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickListener.onRightClick();
                dismiss();
            }
        });
    }

    private void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mTvContent = findViewById(R.id.tv_content);
        mTvLeft = findViewById(R.id.tv_left);
        mView = findViewById(R.id.view);
        mTvRight = findViewById(R.id.tv_right);
        mLay = findViewById(R.id.lay);
        mTvTitle.setText(Html.fromHtml(title));
        mTvContent.setText(content);
        mTvLeft.setText(left);
        mTvRight.setText(right);
    }

    public interface OnClickListener {
        void onLeftClick();

        void onRightClick();
    }

    public CustomDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CustomDialog setContent(String content) {
        this.content = content;
        return this;
    }

    public CustomDialog setLeft(String left) {
        this.left = left;
        return this;
    }

    public CustomDialog setRight(String right) {
        this.right = right;
        return this;
    }

    public CustomDialog setLeftColor(int color) {
        this.leftColor = color;
        return this;
    }

    public CustomDialog setRightColor(int color) {
        this.rightColor = color;
        return this;
    }

    public CustomDialog setOneBtn(boolean oneBtn) {
        this.oneBtn = oneBtn;
        return this;
    }

    public CustomDialog setTitleImage(int image) {
        this.image = image;
        return this;
    }

    public CustomDialog setGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }

    public CustomDialog setWidth(int width) {
        this.width = width;
        return this;
    }

}
